import { defineStore } from 'pinia'
import { ref } from 'vue'


// 定义 Store
export const useCalendarStore = defineStore(
  'calendar',
  () => {

// 定义类型
    type CalendarId = string
    type ItemId = string
    type ItemCalendarMap = Map<ItemId, CalendarId[]>

// 声明响应式变量
    const itemCalendars = ref<ItemCalendarMap>(new Map())

// 新增方法 - 为特定itemId添加calendarId
    const addItemCalendar = (itemId: ItemId, calendarId: CalendarId): void => {
      if (!itemCalendars.value.has(itemId)) {
        itemCalendars.value.set(itemId, [])
      }

      const calendarIds = itemCalendars.value.get(itemId)!
      if (!calendarIds.includes(calendarId)) {
        calendarIds.push(calendarId)
      }
    }

// 批量新增方法 - 为特定itemId添加多个calendarId
    const addItemCalendars = (itemId: ItemId, calendarIds: CalendarId[]): void => {
      if (!itemCalendars.value.has(itemId)) {
        itemCalendars.value.set(itemId, [])
      }

      const existingCalendarIds = itemCalendars.value.get(itemId)!
      calendarIds.forEach(calendarId => {
        if (!existingCalendarIds.includes(calendarId)) {
          existingCalendarIds.push(calendarId)
        }
      })
    }

// 查询方法 - 获取特定itemId关联的所有calendarId
    const getItemCalendars = (itemId: ItemId): CalendarId[] | undefined => {
      return itemCalendars.value.get(itemId)
    }

// 查询方法 - 检查特定itemId是否关联特定calendarId
    const hasItemCalendar = (itemId: ItemId, calendarId: CalendarId): boolean => {
      const calendarIds = itemCalendars.value.get(itemId)
      return calendarIds ? calendarIds.includes(calendarId) : false
    }

// 查询方法 - 获取所有itemId
    const getAllItemIds = (): ItemId[] => {
      return Array.from(itemCalendars.value.keys())
    }

// 查询方法 - 获取特定itemId关联的calendarId数量
    const getItemCalendarCount = (itemId: ItemId): number => {
      const calendarIds = itemCalendars.value.get(itemId)
      return calendarIds ? calendarIds.length : 0
    }

// 删除方法 - 移除特定itemId关联的特定calendarId
    const removeItemCalendar = (itemId: ItemId, calendarId: CalendarId): boolean => {
      const calendarIds = itemCalendars.value.get(itemId)
      if (calendarIds) {
        const index = calendarIds.indexOf(calendarId)
        if (index > -1) {
          calendarIds.splice(index, 1)
          return true
        }
      }
      return false
    }

// 清空某个itemId的所有calendarId关联
    const clearItemCalendars = (itemId: ItemId): void => {
      itemCalendars.value.set(itemId, [])
      // 再删除 key
      itemCalendars.value.delete(itemId)
    }

// 清空所有数据
    const clearAllItemCalendars = (): void => {
      itemCalendars.value.clear()
    }

// 获取特定itemId关联的所有calendarId（去重版本）
    const getUniqueItemCalendars = (itemId: ItemId): CalendarId[] => {
      const calendarIds = itemCalendars.value.get(itemId)
      return calendarIds ? [...new Set(calendarIds)] : []
    }


    return {
      addItemCalendar,
      clearItemCalendars,
      getItemCalendars,

    }
  },
  // 配置持久化
  {
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)